package com.example.wiki.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wiki.entity.Ebook;
import com.example.wiki.entity.User;
import com.example.wiki.entity.req.UserLoginReq;
import com.example.wiki.entity.req.UserQueryReq;
import com.example.wiki.entity.req.UserResetPasswordReq;
import com.example.wiki.entity.req.UserSaveReq;
import com.example.wiki.entity.resp.CommonResp;
import com.example.wiki.entity.resp.PageResp;
import com.example.wiki.entity.resp.UserLoginResp;
import com.example.wiki.entity.resp.UserQueryResp;
import com.example.wiki.service.IUserService;
import com.example.wiki.util.CopyUtil;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author jerry
 * @since 2024-10-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    //
    @Autowired
    IUserService userService;

    //日志的对象获取
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    //依赖redisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    //清空用户数据及token信息
    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token) {
        //删除user的token
        Boolean delete = redisTemplate.delete(token);
        LOG.info("从redis中删除token:{}", token);
        CommonResp resp = new CommonResp<>();
        resp.setSuccess(delete);
        return resp;
    }

    //定义一个http请求接口【登录请求接口】
    @PostMapping("/userLogin")
    public CommonResp userLogin(@Valid @RequestBody UserLoginReq req) {
        CommonResp resp = new CommonResp<>();
        UserLoginResp userLoginResp = userService.userLogin(req);
        resp.setContent(userLoginResp);
        return resp;
    }

    @GetMapping("/getUserListByPage")
    public CommonResp getUserListByPage(@Valid UserQueryReq req) {
        PageResp<UserQueryResp> respPage = userService.getUserListByPage(req);
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        resp.setContent(respPage);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req) {
        User User = CopyUtil.copy(req, User.class);
        //调用业务操作方法
        boolean save = userService.saveUser(req);
        CommonResp resp = new CommonResp<>();
        resp.setSuccess(save);
        return resp;
    }

    @GetMapping("/remove")
    public CommonResp remove(int id) {
        boolean remove = userService.removeById(id);
        CommonResp resp = new CommonResp<>();
        resp.setSuccess(remove);
        return resp;
    }

    @PostMapping("/resetPassword")
    public CommonResp resetPassword(@Valid @RequestBody UserResetPasswordReq req){
        userService.resetPassword(req);
        CommonResp resp = new CommonResp<>();
        return resp;
    }

}
